package com.guaoran.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guaoran.jpa.entity.VpgMovie;
import com.guaoran.jpa.entity.VpgPackage;
import com.guaoran.jpa.repository.VpgMovieRepository;
import com.guaoran.jpa.repository.VpgPackageRepository;
import com.guaoran.mybatis.bean.Demo;
import com.guaoran.mybatis.bean.Dept;
import com.guaoran.mybatis.mapper.DemoMapper;
import com.guaoran.mybatis.mapper.DeptMapper;
import com.guaoran.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author : 孤傲然
 * @Description :
 *  Demo Service实现
 * @date :2018/1/23 21:34
 */
@Service
public class DemoServiceImpl implements DemoService {
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private DemoMapper demoMapper;
    @Autowired
    private VpgPackageRepository vpgPackageRepository;
    @Autowired
    private VpgMovieRepository vpgMovieRepository;

    @Override
    public com.guaoran.utils.PageInfo<Demo> queryDemoByPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Demo> demoList = demoMapper.queryDemoByPage();
        return new com.guaoran.utils.PageInfo<Demo>(demoList);
    }

    @Override
    public PageInfo<Demo> queryDemoByHelperPageOrderBy(int pageNum, int pageSize, String orderBy) {
        PageHelper.startPage(pageNum, pageSize,orderBy);
        List<Demo> demoList = demoMapper.queryDemoByPage();
        return new PageInfo<Demo>(demoList);
    }

    @Override
    public PageInfo<Demo> queryDemoByHelperPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Demo> demoList = demoMapper.queryDemoByPage();
        return new PageInfo<Demo>(demoList);
    }

    @Override
    public List<VpgPackage> findVpgPackagesByProviderId(String providerId) {
        return vpgPackageRepository.findVpgPackagesByProviderId(providerId);
    }

    @Override
    public List<VpgPackage> findVpgPackagesByAssetIdContaining(String assetId) {
        return vpgPackageRepository.findVpgPackagesByAssetIdContaining(assetId);
    }

    @Override
    public List<VpgPackage> findByAssetIdContainingOrAssetNameContaining(String assetId, String assetName) {
        return vpgPackageRepository.findByAssetIdContainingOrAssetNameContaining(assetId,assetName);
    }

    @Override
    public Page<VpgPackage> findAllByProviderIdOrderByAssetName(String providerId, Pageable pageable) {
        return vpgPackageRepository.findAllByProviderIdOrderByAssetName(providerId,pageable);
    }

    @Override
    public Page<VpgPackage> findAllByProviderId(String providerId, Pageable pageable) {
        return vpgPackageRepository.findAllByProviderId(providerId,pageable);
    }

    @Override
    @Transactional
    public VpgPackage saveVpgPackage(VpgPackage vpgPackage) {
        return vpgPackageRepository.save(vpgPackage);
    }

    @Override
    @Transactional
    public VpgMovie saveVpgMovie(VpgMovie vpgMovie) {
        return vpgMovieRepository.save(vpgMovie);
    }

    @Override
    public Page<VpgMovie> findDistinctByTitleContainingOrActorsContainingOrDirectorContaining(String title, String actors, String director, Pageable pageable) {
        return vpgMovieRepository.findDistinctByTitleContainingOrActorsContainingOrDirectorContaining( title, actors, director, pageable);
    }

    @Override
    @Transactional
    public void insertDeptByObject(Dept dept) {
        deptMapper.insertDeptByObject(dept);
    }

    @Override
    @Transactional
    public void insertDeptByMap(Map<String, Object> map) {
        deptMapper.insertDeptByMap(map);
    }

    @Override
    public List<Dept> queryDeptsByName(String name) {
        return deptMapper.queryDeptsByName(name);
    }

    @Override
    @Transactional
    public int deleteDeptByObject(Long id) {
        return deptMapper.deleteDeptByObject(id);
    }

    @Override
    @Transactional
    public int updateDeptAgeByName(Integer age, String name) {
        return deptMapper.updateDeptAgeByName(age,name);
    }

    @Override
    public List<Demo> findAllDemos() {
        return demoMapper.findAllDemos();
    }

    @Override
    @Transactional
    public void insertDemo(Demo demo) {
        demoMapper.insertDemo(demo);
    }

    @Override
    public List<Demo> queryDemosByName(String name) {
        return demoMapper.queryDemosByName(name);
    }

    @Override
    public VpgMovie queryMovieById(Long id) {
        return vpgMovieRepository.findOne(id);
    }

    @Override
    public VpgPackage queryPackageById(Long id) {
        return vpgPackageRepository.findOne(id);
    }

    @Override
    @Transactional
    public void deleteMovieById(Long id) {
        vpgMovieRepository.delete(id);
    }

    @Override
    @Transactional
    public void deletePackageById(Long id) {
        vpgPackageRepository.delete(id);
        List<VpgMovie> movieList = vpgMovieRepository.queryByPackageId(id);
        vpgMovieRepository.delete(movieList);
    }

    @Override
    public List<VpgMovie> queryByPackageId(Long packageId) {
        return vpgMovieRepository.queryByPackageId(packageId);
    }
}
